<template>
	<view>
		<view class="search">
			<uni-search-bar @confirm="search" placeholder="搜索商家店名" @cancel="cancel"></uni-search-bar>
		</view>
		<swiper circular autoplay :interval="3000" :duration="500" indicator-dots style="height: 350rpx;"
			indicator-color="rgba(255, 255, 255, 0.6)" indicator-active-color="#3CB371">
			<swiper-item v-for="item in banners" :key="item.id">
				<navigator :url="'/pages/Detail/Detail?businessId='+item.businessId" hover-class="navigator-hover">
					<image :src="item.img" alt="" mode="widthFix" style="width: 100%;" />
				</navigator>
			</swiper-item>
		</swiper>

		<view style="padding: 20rpx;">
			<!-- 公告 -->
			<view class="box">
				<uni-icons type="sound" size="20" style="position: relative; top: 5rpx;"></uni-icons>
				<text style="margin-left: 5rpx;">{{ notice }}</text>
			</view>



			<!-- 商家分类 -->
			<view class="box" style="margin: 20rpx 0;">
				<view style="display: flex;">
					<view @click="showAccordType(item.type)" class="category-item" v-for="item in categoryList" :key="item.id" style="width: 70px;height: 120px;">
						<image :src="item.avatar" mode="widthFix" style="width: 70px;height: 70px;"></image>
						<text>{{item.type}}</text>
					</view>
				</view>
			</view>



			<!-- 商家列表 -->
			<view class="box" style="color: orange; font-size: 32rpx; font-weight: bold; margin-bottom: 10rpx;">
				热门商家
			</view>
	
			<view>
				<scroll-view :scroll-top="scrollTop" scroll-y="true"    style="height: 70vh;">
				<view class="box" v-for="item in businessList" :key="item.id"
					style="display: flex; grid-gap: 30rpx; margin-bottom: 10rpx;"  @click="goDetail(item.id,item.isCollect)">
					
					
					<view style="width: 30%;">
						<image :src="item.avatar" style="width: 100%; height: 200rpx; border-radius: 10rpx; display: block;"></image>
					</view>
					
					
					<view style="flex: 1; display: flex; flex-direction: column; justify-content: space-between; grid-gap: 10rpx;">
						
					<view style="font-size: 36rpx; font-weight: bold;">{{ item.name }}</view>

						<view style="display: flex;  color: #666; ">
							<view style="flex: 1;">
								<text v-if="item.start !== 'NaN'" style="color: #ff9800; font-weight: bold;">{{item.start}}分</text>
								<text v-if="item.start == 'NaN'" style="color: #ff9800; font-weight: bold;">0.0分</text>
								<text style="margin-left: 10rpx;">已售{{item.count}}</text>
							</view>
							<view style="flex: 1; text-align: right;">30分钟内送达</text>
							</view>
						</view>
						
						<view style="color: #ff9800;">{{item.info}}</view>
						
						<view style="background-color: #ffd281; color: brown; border-radius: 4rpx; width: fit-content;
							padding: 0 8rpx;">{{ item.type || '商家介绍' }}</view>

					 </view>
				
					
					
				</view>
				</scroll-view>
			</view>
		
		</view>
	</view>
</template>

<script>
	import '@/static/css/global.css'
	export default {
		data() {
			return {
				banners: [],
				noticeList: [],
				notice: '',
				businessList: [],
				user:uni.getStorageSync('xm-user'),
				categoryList:[],
				historyObj:{},
			}
		},
		onLoad(option) {
		
			this.load()
		},
		onShow(option){
		
		},
		
		methods: {
			cancel(){
				// 获取商家的列表
				this.$request.get('/business/selectAll/'+this.user.id, {
					data: {
						status: '通过'
					}
				}).then(res => {
					this.businessList = res.data || []
				})
			},
			showAccordType(businessType){
				this.$request.get('/category/showAccordType/'+businessType+"/"+this.user.id).then(res => {
						this.businessList = res.data || []
				})
			},
			search(res) {
			  let value = res.value
				this.$request.get('/business/searchByBusinessName/'+value+"/"+this.user.id).then(res => {
					if(res.data.length == 0){
						uni.showToast({
						  icon: 'error',
						  title: '未找到该商家！'
						})
					}else{
						this.businessList = res.data || []
						}
					
				})
			},
			load() {
				// 获取轮播图数据
				this.$request.get('/banner/selectAll').then(res => {
					this.banners = res.data || []
				})

				// 获取公告数据
			this.$request.get('/notice/selectAll').then(res=>{
				this.noticeList  = res.data
				this.notice = this.noticeList[0]?.title
			})
			let index = 0;
			 
				setInterval(res =>{
					if(index < this.noticeList.length ){
						this.notice = this.noticeList[index++].title		
					}
				},3000)
	
			//获取商品分类列表
			this.$request.get('/category/selectAll/').then(res => {
				this.categoryList = res.data || []
			})

				// 获取商家的列表
				this.$request.get('/business/selectAll/'+this.user.id, {
					data: {
						status: '通过'
					}
				}).then(res => {
					this.businessList = res.data || []
				})
			},
			goDetail(businessID,isCollect){
				
				this.historyObj.userId = this.user.id
				this.historyObj.businessId = businessID
				//存到历史记录
				this.$request.post('/history/add/',this.historyObj).then(res=>{
					
				})
				
				uni.navigateTo({
					url:"/pages/Detail/Detail?businessId="+businessID+"&isCollect="+isCollect
				})
			}
			
			
		}
	}
</script>

<style>
	.category-item {
		flex: 1;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		align-items: center;
		grid-gap: 10rpx;
	}
</style>